package service

import (
	appsV1 "k8s.io/api/apps/v1"
	coreV1 "k8s.io/api/core/v1"
	"sort"
	"strings"
	"time"
)

type FilterQuery struct {
	Name string
}

type PageQuery struct {
	Limit int
	Page  int
}

// DataSelectorQuery 实现数据分页使用
type DataSelectorQuery struct {
	Filter   *FilterQuery
	Paginate *PageQuery
}

type DataCell interface {
	GetCreation() time.Time
	GetName() string
}

// 数据封装
type dataSelector struct {
	GenericDataList []DataCell
	DataSelectQuery *DataSelectorQuery
}

// Len 获取数组长度
func (d *dataSelector) Len() int {
	return len(d.GenericDataList)
}

// Swap 位置交换
func (d *dataSelector) Swap(i, j int) {
	d.GenericDataList[i], d.GenericDataList[j] = d.GenericDataList[j], d.GenericDataList[i]
}

// Less 比较大小
func (d *dataSelector) Less(i, j int) bool {
	a := d.GenericDataList[i].GetCreation()
	b := d.GenericDataList[j].GetCreation()
	return b.Before(a)
}

// Sort 排序
func (d *dataSelector) Sort() *dataSelector {
	sort.Sort(d)
	return d
}

// Filter 用于过滤数据，比较数据的name属性，若包含，则返回
func (d *dataSelector) Filter() *dataSelector {
	if d.DataSelectQuery.Filter.Name == "" {
		return d
	}
	//过滤不为空
	var filtered []DataCell
	for _, v := range d.GenericDataList {
		objName := v.GetName()
		if !strings.Contains(objName, d.DataSelectQuery.Filter.Name) {
			continue
		}
		filtered = append(filtered, v)
	}
	d.GenericDataList = filtered
	return d
}

// Paginate 方法用于数组的分页，根据limit与page的传参取一定范围内的数据返回
func (d *dataSelector) Paginate() *dataSelector {
	limit := d.DataSelectQuery.Paginate.Limit
	page := d.DataSelectQuery.Paginate.Page
	if limit <= 0 || page <= 0 {
		return d
	}
	startIndex := (page - 1) * limit
	endIndex := limit*page - 1
	if startIndex > len(d.GenericDataList) {
		startIndex = 0
	}
	if endIndex > len(d.GenericDataList) {
		endIndex = len(d.GenericDataList)
	}
	d.GenericDataList = d.GenericDataList[startIndex:endIndex]
	return d
}

type podCell coreV1.Pod

func (p podCell) GetCreation() time.Time {
	return p.CreationTimestamp.Time
}

func (p podCell) GetName() string {
	return p.Name
}

type nodeCell coreV1.Node

func (p nodeCell) GetCreation() time.Time {
	return p.CreationTimestamp.Time
}

func (p nodeCell) GetName() string {
	return p.Name
}

type deployCell appsV1.Deployment

func (d deployCell) GetCreation() time.Time {
	return d.CreationTimestamp.Time
}

func (d deployCell) GetName() string {
	return d.Name
}

type nsCell coreV1.Namespace

func (d nsCell) GetCreation() time.Time {
	return d.CreationTimestamp.Time
}

func (d nsCell) GetName() string {
	return d.Name
}

type dsCell appsV1.DaemonSet

func (d dsCell) GetCreation() time.Time {
	return d.CreationTimestamp.Time
}

func (d dsCell) GetName() string {
	return d.Name
}

type svcCell coreV1.Service

func (d svcCell) GetCreation() time.Time {
	return d.CreationTimestamp.Time
}

func (d svcCell) GetName() string {
	return d.Name
}

type stsCell appsV1.StatefulSet

func (d stsCell) GetCreation() time.Time {
	return d.CreationTimestamp.Time
}

func (d stsCell) GetName() string {
	return d.Name
}
